import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QDesktopWidget
from PyQt5.QtGui import QIcon

class CenterForm(QMainWindow):
    def __init__(self,parent=None):
        super(CenterForm,self).__init__(parent)

        #设置主窗口标题
        self.setWindowTitle('窗口居中显示')

        #设置主窗口尺寸
        self.resize(400,300)
    def center(self):
        #得到屏幕的坐标系
        screen=QDesktopWidget().screenGeometry()
        #得到窗口的坐标系
        size=self.geometry()

        '''
        01重点在这里
        '''
        newLeft=(screen.width()-size.width())/2
        newTop=(screen.height()-size.height())/2
        self.move(newLeft,newTop)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    ui_class=CenterForm()
    '''
    02重点在这里
    '''
    ui_class.center()


    ui_class.show()
    sys.exit(app.exec_())